// pages/my/add-bank/index.js
const http = require('../../../../utils/httpUtils.js')
import {
    cityData
} from '../../../../utils/city.js'
const RSA = require("../../../../utils/wx_rsa.js");
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '添加银行卡' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        prompt: '',
        addresId: [],
        bankType: '',
        bankList: [{
            "b_id": 1,
            "bank_name": "招商银行",
            "bank_code": "CMB"
        }, {
            "b_id": 2,
            "bank_name": "建设银行",
            "bank_code": "CCB"
        }, {
            "b_id": 3,
            "bank_name": "交通银行",
            "bank_code": "BOCOM"
        }, {
            "b_id": 4,
            "bank_name": "邮储银行",
            "bank_code": "PSBC"
        }, {
            "b_id": 5,
            "bank_name": "工商银行",
            "bank_code": "ICBC"
        }, {
            "b_id": 6,
            "bank_name": "农业银行",
            "bank_code": "ABC"
        }, {
            "b_id": 7,
            "bank_name": "中国银行",
            "bank_code": "BOC"
        }, {
            "b_id": 8,
            "bank_name": "中信银行",
            "bank_code": "CITIC"
        }, {
            "b_id": 9,
            "bank_name": "光大银行",
            "bank_code": "CEB"
        }, {
            "b_id": 10,
            "bank_name": "华夏银行",
            "bank_code": " HXB"
        }, {
            "b_id": 11,
            "bank_name": "民生银行",
            "bank_code": "CMSB"
        }, {
            "b_id": 12,
            "bank_name": "广发银行",
            "bank_code": "CGB"
        }, {
            "b_id": 13,
            "bank_name": "平安银行",
            "bank_code": "SZD"
        }, {
            "b_id": 14,
            "bank_name": "浙商银行",
            "bank_code": "CZB"
        }, {
            "b_id": 15,
            "bank_name": "浦东发展银行",
            "bank_code": "SPDB"
        }]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.getStorage({
            key: 'userInfo',
            success: res => {
                this.setData({
                    phone: res.data.phone
                })
            }
        })
    },
    getCode() { //获取验证码
        http._headerPost('/api/sendsms/sendsms', {
            phone: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.phone}`)),
            scene: 'bank_card',
            client: 'weixinSmall'
        }, res => {
            if (res.data.code == 1) {
                wx.showToast({
                    title: '获取成功',
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
        })
    },
    //添加银行卡
    addBankCard(data) {
        http._headerPost('/api/UserBankCard/add', data, res => {
            if (res.data.code == 1) {
                wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    showCancel: false,
                    success: res => {
                        wx.navigateBack({})
                    }
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
        })
    },
    // 提交表单  
    subCardInfo(e) {
        let data = e.detail.value
        if (data.holder == '') {
            wx.showToast({
                title: '请输入持卡人姓名',
                icon: 'none'
            })
            return
        }
        if (data.cardNum != data.cardNumConfirm) {
            wx.showToast({
                title: '两个卡号不一致',
                icon: 'none'
            })
            return
        }
        if (this.data.bankType == '') {
            wx.showToast({
                title: '暂不支持该卡类型',
                icon: 'none'
            })
            return
        }
        data.phone = this.data.phone;
        data.b_id = this.data.bankType;

        this.addBankCard(data)
    },
    //  绑定手机号
    phone(e) {
        this.setData({
            phone: e.detail.value
        })
    },

    //选择地址
    bindAddress(e) {
        this.setData({
            province: e.detail.value[0],
            citys: e.detail.value[1],
            county: e.detail.value[2],
            prompt: e.detail.value[0] + ' ' + e.detail.value[1] + ' ' + e.detail.value[2]
        })
        this.addressToCode()
        console.log(this.data.addresId)
    },
    //查询地址ID
    addressToCode() {
        cityData.forEach(province => {
            if (province.name == this.data.province) {
                console.log(province.name)
                this.data.addresId[0] = province.id
                province.cityList.forEach(citys => {
                    if (citys.name == this.data.citys) {
                        this.data.addresId[1] = citys.id
                        citys.districtList.forEach(county => {
                            if (county.name == this.data.county) {
                                this.data.addresId[2] = county.id
                                const city = require('../../../../utils/city.js')
                            }
                        })
                    }
                })
            }
        })
    },
    choiceBank(e) {
        this.setData({
            choBank: this.data.bankList[e.detail.value].bank_name,
            bankType: this.data.bankList[e.detail.value].b_id
        })
    }
})